package com.multysite.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.regex.Pattern;

import javax.servlet.ServletContext;

public class Utils {
	public static String getIdDomain(String hotsName) {
		return hotsName.replace("." + Utils.domain(), "");
	}

	public static String domain() {
		// return "multywebsite.appspot.com";
		return "multysite.com";
	}

	public static String getAliasDetail(String str) {
		if (str != null)
			str = str.replace(".html", "");
		else
			str = "";
		return str;
	}

	public static String getContentPhp(String pathFile,
			ServletContext srevletContext) {
		InputStream is = srevletContext.getResourceAsStream(pathFile);
		StringBuffer read = new StringBuffer();
		try {
			InputStreamReader in = new InputStreamReader(is, "utf-8");

			char[] buf = new char[102102];

			int numRead = 0;
			while ((numRead = in.read(buf)) != -1) {
				String readData = String.valueOf(buf, 0, numRead);
				read.append(readData);
				buf = new char[102102];
			}
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return read.toString();
	}

	public static String returnServletFromUrl(String url) {
		String namServlet = "";
		if (!url.equals("/create")) {
			if (Pattern.matches("^[\\w\\W]+.html$", url)) {
				namServlet = "/detail";
			} else if (url.startsWith("/tag/")) {
				namServlet = "/tag";
			} else if (url.startsWith("/applications")) {
				namServlet = url;
			} else if (url.startsWith("/admin")) {
				namServlet = url;
			} else if (!url.equals("/")) {
				namServlet = "/category";
			} else {
				namServlet = "/home";
			}
		} else {
			namServlet = url;
		}
		return namServlet;
	}
}
